package com.csaba.account.overview;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.part.EditorPart;

import com.csaba.account.AccountPlugin;

public class AccountOverviewEditor extends EditorPart
{
	@Override
	public void doSave( IProgressMonitor arg0 )
	{
	}

	@Override
	public void doSaveAs()
	{
	}

	@Override
	public void init( IEditorSite site, IEditorInput input ) throws PartInitException
	{
		setSite(site);
		setInput(input);
	}

	@Override
	public boolean isDirty()
	{
		return false;
	}

	@Override
	public boolean isSaveAsAllowed()
	{
		return false;
	}

	@Override
	public void setFocus()
	{

	}

	private class AccountsSelectionChangedListener implements ISelectionChangedListener
	{

		@Override
		public void selectionChanged( SelectionChangedEvent arg0 )
		{
			IStructuredSelection selection = (IStructuredSelection) arg0.getSelection();
			Account account = (Account) selection.getFirstElement();

			lastTransactionsTable.setInput(account);
			bank.setText(account.bank);
			accountNo.setText(account.accountNo);
			available.setText(Integer.toString(account.availableBalance));
			bookBalance.setText(Integer.toString(account.bookBalance));
			overdraftLimit.setText(Integer.toString(account.overdraftLimit));
		}

	};

	AccountsSelectionChangedListener accountsSelectionChangedListener = new AccountsSelectionChangedListener();

	@Override
	public void createPartControl( Composite parent )
	{
		FormToolkit toolkit;
		ScrolledForm form;

		toolkit = new FormToolkit(parent.getDisplay());
		form = toolkit.createScrolledForm(parent);
		form.setText("Account Overview");

		form.setBackgroundImage(AccountPlugin.getDefault().getImage(AccountPlugin.IMG_FORM_BG));

		GridLayout layout = new GridLayout();
		layout.horizontalSpacing = 10;
		layout.verticalSpacing = 10;
		layout.numColumns = 2;

		form.getBody().setLayout(layout);

		initAccountsSection(toolkit, form);
		initSummarySection(toolkit, form);
		initAccountDetailsSection(toolkit, form);
		initLastTransactionsSection(toolkit, form);

		accountsTable.setSelection(new StructuredSelection(AccountsContentProvider.accounts[0]));
	}

	private TableViewer lastTransactionsTable;

	private void initLastTransactionsSection( FormToolkit toolkit, ScrolledForm form )
	{
		// Setup section
		Section section = toolkit.createSection(form.getBody(), ExpandableComposite.EXPANDED
				| ExpandableComposite.TITLE_BAR);
		section.setText("Last 10 transactions");

		GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
		layoutData.horizontalSpan = 2;
		section.setLayoutData(layoutData);

		// Setup section contents
		Composite client = toolkit.createComposite(section);
		client.setLayout(new GridLayout(1, true));

		Table table = toolkit.createTable(client, SWT.SINGLE | SWT.FULL_SELECTION);
		layoutData = new GridData(GridData.FILL_BOTH);
		layoutData.heightHint = 141;
		table.setLayoutData(layoutData);

		TableColumn column = new TableColumn(table, SWT.LEFT);
		column.setText("Date");
		column.setWidth(80);

		column = new TableColumn(table, SWT.LEFT);
		column.setText("Target Account");
		column.setWidth(100);

		column = new TableColumn(table, SWT.LEFT);
		column.setText("Details");
		column.setWidth(250);

		column = new TableColumn(table, SWT.RIGHT);
		column.setText("Amount");
		column.setWidth(60);

		column = new TableColumn(table, SWT.LEFT);
		column.setText("Currency");
		column.setWidth(60);

		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		lastTransactionsTable = new TableViewer(table);
		lastTransactionsTable.setContentProvider(new LastTransactionsContentProvider());
		lastTransactionsTable.setLabelProvider(new LastTransactionsLabelProvider());
		lastTransactionsTable.setInput(AccountsContentProvider.accounts[0]);

		toolkit.paintBordersFor(client);
		section.setClient(client);
	}

	private Text bank;
	private Text accountNo;
	private Text available;
	private Text overdraftLimit;
	private Text bookBalance;

	private void initAccountDetailsSection( FormToolkit toolkit, ScrolledForm form )
	{
		Section section = toolkit.createSection(form.getBody(), ExpandableComposite.EXPANDED
				| ExpandableComposite.TITLE_BAR);
		section.setText("Account details");

		GridData layoutData = new GridData(GridData.FILL_BOTH);
		section.setLayoutData(layoutData);

		Composite client = toolkit.createComposite(section);

		GridLayout gl = new GridLayout();
		gl.numColumns = 2;
		gl.horizontalSpacing = 10;
		gl.verticalSpacing = 5;
		client.setLayout(gl);

		toolkit.createLabel(client, "Bank:");
		bank = toolkit.createText(client, "");
		bank.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		bank.setEditable(false);

		toolkit.createLabel(client, "Account No:");
		accountNo = toolkit.createText(client, "");
		accountNo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		bank.setEditable(false);

		toolkit.createLabel(client, "Available Balance:");
		available = toolkit.createText(client, "", SWT.RIGHT);
		available.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		bank.setEditable(false);

		toolkit.createLabel(client, "Book Balance:");
		bookBalance = toolkit.createText(client, "", SWT.RIGHT);
		bookBalance.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		bank.setEditable(false);

		toolkit.createLabel(client, "Overdraft Limit:");
		overdraftLimit = toolkit.createText(client, "", SWT.RIGHT);
		overdraftLimit.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		bank.setEditable(false);

		toolkit.paintBordersFor(client);
		section.setClient(client);
	}

	private void initSummarySection( FormToolkit toolkit, ScrolledForm form )
	{
		Section section = toolkit.createSection(form.getBody(), ExpandableComposite.EXPANDED
				| ExpandableComposite.TITLE_BAR);
		section.setText("Summary");

		GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
		section.setLayoutData(layoutData);

		Composite client = toolkit.createComposite(section);

		GridLayout twl = new GridLayout();
		twl.numColumns = 2;
		twl.horizontalSpacing = 10;
		twl.verticalSpacing = 5;
		client.setLayout(twl);

		Label availableFunds;
		Label financialPosition;

		toolkit.createLabel(client, "Available Funds:");
		availableFunds = toolkit.createLabel(client, "");
		availableFunds.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		availableFunds.setAlignment(SWT.RIGHT);

		toolkit.createLabel(client, "Financial Position:");
		financialPosition = toolkit.createLabel(client, "");
		financialPosition.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		financialPosition.setAlignment(SWT.RIGHT);

		section.setClient(client);

		int af = 0;
		int fp = 0;
		for ( Account a : AccountsContentProvider.accounts )
		{
			af += a.availableBalance;
			fp += a.bookBalance;
		}

		availableFunds.setText(Integer.toString(af));
		financialPosition.setText(Integer.toString(fp));
	}

	private TableViewer accountsTable;

	private void initAccountsSection( FormToolkit toolkit, ScrolledForm form )
	{
		// Setup section
		Section section = toolkit.createSection(form.getBody(), ExpandableComposite.EXPANDED
				| ExpandableComposite.TITLE_BAR);
		section.setText("Accounts");

		GridData layoutData = new GridData(GridData.FILL_BOTH);
		layoutData.verticalSpan = 2;
		section.setLayoutData(layoutData);

		// Setup section contents
		Composite client = toolkit.createComposite(section);
		client.setLayout(new GridLayout(1, true));

		Table table = toolkit.createTable(client, SWT.SINGLE | SWT.FULL_SELECTION);
		table.setLayoutData(new GridData(GridData.FILL_BOTH));

		TableColumn column = new TableColumn(table, SWT.CENTER);
		column.setText("B");
		column.setWidth(24);
		column.setAlignment(SWT.CENTER);

		column = new TableColumn(table, SWT.CENTER);
		column.setText("T");
		column.setWidth(24);

		column = new TableColumn(table, SWT.LEFT);
		column.setText("Account");
		column.setWidth(80);

		column = new TableColumn(table, SWT.RIGHT);
		column.setText("Available Balance");
		column.pack();

		column = new TableColumn(table, SWT.RIGHT);
		column.setText("Book Balance");
		column.setWidth(80);

		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		accountsTable = new TableViewer(table);
		accountsTable.setContentProvider(new AccountsContentProvider());
		accountsTable.setLabelProvider(new AccountsLabelProvider());
		accountsTable.setInput(new Object());
		accountsTable.addSelectionChangedListener(accountsSelectionChangedListener);

		toolkit.paintBordersFor(client);
		section.setClient(client);
	}
}
